
// const HOST="http://47.108.85.176";
// const DOC="http://47.108.85.176";
const HOST="http://192.168.2.171:8080";
const DOC="http://192.168.2.171";

function doPost(url,params,suc,fail,finish) {
    let formData = new FormData();
    for(let key in params){
        formData.append(key,params[key]);
    }
    const token = sessionStorage.getItem("token");
    if(token==null){
        finish();
        window.location.href="./static/login.html";
        return;
    }
    $.ajax({
        url:HOST+url,
        async:true,
        type: "POST",
        timeout:1000*15,
        dataType: "json",
        headers:{
            "token":token
        },
        processData: false,
        contentType: false,
        data:formData,
        success:function (data, textStatus, jqXHR) {

            let tk = jqXHR.getResponseHeader("token");
            console.log("token",tk)
            if(tk!=null){
                sessionStorage.setItem("token",tk);
            }
            suc(data);
        },
        error:function (err) {
            fail(err);
        },
        complete:function () {
            finish();
        }
    });
}

function doGet(url,params,suc,fail,finish) {
    let formData = "{";
    for (let key in params) {
        formData = formData + key + ":" + params[key] + ","
    }
    formData = formData + "}"
    var token = sessionStorage.getItem("token")
    $.ajax({
        url: HOST + url,
        async: true,
        type: "GET",
        timeout: 1000 * 15,
        dataType: "json",
        headers: {
            "token": token
        },
        processData: false,
        contentType: false,
        data: formData,
        success: suc,
        error: fail,
        complete: finish
    });
}

//建立一個可存取到該file的url
function getObjectURL(file) {
    let url = null ;
    if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}

Date.prototype.Format = function(fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}



